import store from '@/store/index'
import tools from '@/common/tools'
import * as api from '@/api/index'
import  thirdApi from '@/api/third'
export default{
    wx_login_info(){
        return new Promise(resolve => {
            let wx_login_info = store.getters.wx_login_info
            console.log('wx_login_info')
            if(!tools.isEmpty(wx_login_info)){
                console.log('直接拿')
                //检测是否失效
                thirdApi.checkSession().then(res=> {
                    if(res){
                        console.log('没有失效')
                        resolve(wx_login_info)
                    }else{
                        console.log('失效')
                        //失效
                        store.dispatch('wxLoginInfo/login').then(res=>{
                            if(res)  resolve(res)
                        })
                    }
                })

            }else{
                console.log('没有接口继续获取')

                store.dispatch('wxLoginInfo/login').then(res=>{
                    if(res) resolve(res)
                })
            }
        })
    },
    //获取到用户信息
    userInfo(){
        return new Promise(resolve => {
            if(!tools.user() && tools.getToken()){
                store.dispatch('user/getInfo').then(res=>{
                    console.log('stop111...')
                    resolve(res)
                })
            }else if(tools.user()){
                console.log('stop222...')
                console.log(tools.user())
                resolve(tools.user())
            }else{
                console.log('stop333...')
            }
        })
    },
    forceUpdate() {
        return new Promise(resolve => {
            store.dispatch('user/getInfo',{forceUpdate:true}).then(res=>{
                resolve(res)
            })
        })

    },
    isLogin(){
        return tools.getToken() && tools.user() ? true : false
    },
    autoLogin(){
        let self = this
        return new Promise(resolve => {
            let wxlogininfo = store.getters.wx_login_info
           // 未登录自动登录情况下
            if(!this.isLogin() && wxlogininfo.token ){
                self.userInfo().then(res=>{
                    if(res){
                        console.log('自动登录成功')
                        resolve(res);
                    }
                })
            }else if(this.isLogin()){
                console.log('不需要自动登录啦111~~~~~~');
                resolve(tools.user())
            }else{
                console.log(store.getters);
                console.log('不需要自动登录啦222~~~~~~');
                resolve(false)
            }
        })
    },
    //用户信息
    user(){
        return store.getters.user
    },
    async test(){
        return new Promise(resolve => {
            resolve('tanglewang')
        })
    }

}
